'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 联盟商城购买 接口
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../../consts/code');
var unionGroupConf = require('../../../../config/unionGroupConf');
var unionShopConf = require('../../../../config/unionShopConf.js');
var unionGroupModel = require('../../../../models/unionGroupModel');
var unionPositionType = require('../../../../consts/unionPositionType');
var PlayerManager = require('../../../../libs/playerManager');
var rewardService = require('../../../../services/rewardService.js');
var moduleType = require('../../../../consts/moduleType.js');
var operationType = require('../../../../consts/operationType.js');

/**
 * 联盟商城购买
 */

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var shop_id = msg.shop_id;

    if (!shop_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    var union_group_model;
    var union_id;
    var union_task_model;
    var reward_list;
    var shop_list;
    var buy_num_list = [];
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                ['role', 'unionTask', 'bag', 'build', 'task', 'daily_task', 'achieve', 'tech', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                union_id = role_model.getUnionId();

                if (!!union_id) {
                    unionGroupModel.getById(union_id, function (err, model) {
                        union_group_model = model;
                        cb();
                    });
                } else {
                    return next(null, {
                        code: Code.UNION_IS_NOT_JOIN
                    });
                }
            },
            function (cb) {
                union_task_model = player_manager.unionTask;
                reward_list = unionShopConf.getConfById(shop_id);

                var union_lv = union_task_model.lv;

                // 是否可以购买(联盟等级是否达到)
                var need_union_lv = reward_list.lvl;
                if (union_lv < need_union_lv) {
                    return next(null, {
                        code: Code.UNION_SHOP_BUY_UNION_LV_NOT
                    });
                }

                // 是否还有购买次数

                var num = union_task_model.getBuyShopNum(shop_id);
                var buy_max_num = reward_list.buy_time;

                if (num > buy_max_num) {
                    return next(null, {
                        code: Code.UNION_SHOP_BUY_UNION_LV_NOT
                    });
                }

                // 消耗声望
                var reputation_num = reward_list.cost_reputation;
                if (!union_task_model.subReputation(reputation_num)) {
                    return next(null, {
                        code: Code.UNION_SHOP_REPUTATION_NOT
                    });
                }

                // 发放奖励
                rewardService.addRewardToRole(player_manager, reward_list, moduleType.MDL_UNION, operationType.OPR_UNION_SHOP_BUY, shop_id);

                // 增加购买次数记录
                union_task_model.addBuyShopNum(shop_id);

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            union_group_model.save();
            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: [reward_list]
                }
            });
        }
    );
};
